package com.java110.mall.cart.bmo.housekeepingServPool.impl;

import com.java110.mall.cart.bmo.housekeepingServPool.ISaveHousekeepingServPoolBMO;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.factory.Java110TransactionalFactory;
import com.java110.dto.app.AppDto;
import com.java110.dto.housekeeping.HousekeepingServPoolDto;
import com.java110.dto.housekeeping.HousekeepingServPoolUserDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.intf.cart.IHousekeepingServPoolInnerServiceSMO;
import com.java110.intf.cart.IHousekeepingServPoolUserInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderCartInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.po.housekeeping.HousekeepingServPoolPo;
import com.java110.po.housekeeping.HousekeepingServPoolUserPo;
import com.java110.po.storeOrder.StoreOrderPo;
import com.java110.po.storeOrder.StoreOrderCartPo;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("saveHousekeepingServPoolBMOImpl")
public class SaveHousekeepingServPoolBMOImpl implements ISaveHousekeepingServPoolBMO {

    @Autowired
    private IHousekeepingServPoolInnerServiceSMO housekeepingServPoolInnerServiceSMOImpl;

    @Autowired
    private IHousekeepingServPoolUserInnerServiceSMO housekeepingServPoolUserInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMO;

    @Autowired
    private IStoreOrderCartInnerServiceSMO storeOrderCartInnerServiceSMO;

    @Autowired
    private IUserV1InnerServiceSMO userInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    /**
     * 添加小区信息
     *
     * @param housekeepingServPoolPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> save(HousekeepingServPoolPo housekeepingServPoolPo) {

        housekeepingServPoolPo.setPoolId(GenerateCodeFactory.getGeneratorId("11"));


        int flag = housekeepingServPoolInnerServiceSMOImpl.saveHousekeepingServPool(housekeepingServPoolPo);

        if (flag > 0) {
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }

    /**
     * 添加电话家政维修
     *
     * @param housekeepingServPoolPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> saveTel(HousekeepingServPoolPo housekeepingServPoolPo, String userId, String userName) {
        housekeepingServPoolPo.setPoolId(GenerateCodeFactory.getGeneratorId("11"));
        housekeepingServPoolPo.setCartId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_cartId));
        housekeepingServPoolPo.setState(HousekeepingServPoolDto.STATE_WAIT);
        housekeepingServPoolPo.setObjId("-1");
        housekeepingServPoolPo.setObjName("-1");
        housekeepingServPoolPo.setObjType("-1");
        int flag = housekeepingServPoolInnerServiceSMOImpl.saveHousekeepingServPool(housekeepingServPoolPo);
        if (flag < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存电话家政服务工单池失败-1");
        }
        //写 用户工单表
        HousekeepingServPoolUserPo housekeepingServPoolUserPo = new HousekeepingServPoolUserPo();
        housekeepingServPoolUserPo.setContext(userName + "提交电话家政订单");
        housekeepingServPoolUserPo.setEndTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
        housekeepingServPoolUserPo.setPoolEvent(HousekeepingServPoolUserDto.POOL_EVENT_START_USER);
        housekeepingServPoolUserPo.setPoolId(housekeepingServPoolPo.getPoolId());
        housekeepingServPoolUserPo.setPreRuId("-1");
        housekeepingServPoolUserPo.setPreStaffId("-1");
        housekeepingServPoolUserPo.setPreStaffName("无");
        housekeepingServPoolUserPo.setRuId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_ruId));
        housekeepingServPoolUserPo.setStaffId(userId);
        housekeepingServPoolUserPo.setStaffName(userName);
        housekeepingServPoolUserPo.setStartTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
        housekeepingServPoolUserPo.setState(HousekeepingServPoolUserDto.STATE_SUBMIT);
        housekeepingServPoolUserPo.setShopId(housekeepingServPoolPo.getShopId());
        flag = housekeepingServPoolUserInnerServiceSMOImpl.saveHousekeepingServPoolUser(housekeepingServPoolUserPo);
        if (flag < 0) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存电话家政服务工单池失败-2");
        }

        StoreOrderPo storeOrderPo = new StoreOrderPo();
        storeOrderPo.setOrderId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_orderId));
        storeOrderPo.setOId(StringUtil.isEmpty(Java110TransactionalFactory.getOId()) ? "-1" : Java110TransactionalFactory.getOId());
        storeOrderPo.setPayPrice("0");
        storeOrderPo.setTotalPrice("0");
        storeOrderPo.setFreightPrice("0");
        storeOrderPo.setState(StoreOrderDto.STATE_FINISH_PAY);
        storeOrderPo.setAppName("商城系统");
        storeOrderPo.setAppId(AppDto.MALL_APP_ID);
        storeOrderPo.setPersonId("-1");
        storeOrderPo.setPersonName(housekeepingServPoolPo.getApplyName());
        storeOrderPo.setServiceFee("0.00");
        storeOrderPo.setDeduction("0");
        storeOrderPo.setShopId(housekeepingServPoolPo.getShopId());
        storeOrderPo.setProfitSharing("N");
        flag = storeOrderInnerServiceSMO.saveStoreOrder(storeOrderPo);
        if (flag < 0) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存电话家政服务工单池失败-3");
        }
        StoreOrderCartPo storeOrderCartPo = new StoreOrderCartPo();
        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(housekeepingServPoolPo.getShopId());
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);

        storeOrderCartPo.setCartId(housekeepingServPoolPo.getCartId());

        storeOrderCartPo.setOrderId(storeOrderPo.getOrderId());

        storeOrderCartPo.setCartNum("1");
        storeOrderCartPo.setFreightPrice("0");
        storeOrderCartPo.setShopId(housekeepingServPoolPo.getShopId());

        storeOrderCartPo.setExtCartId("-1");
        storeOrderCartPo.setPersonId(storeOrderPo.getPersonId());
        storeOrderCartPo.setState(StoreOrderCartDto.STATE_WAIT_SERV_R);
        storeOrderCartPo.setStoreId(shopDtos.get(0).getStoreId());

        storeOrderCartPo.setPayPrice("0");
        storeOrderCartPo.setProductId(housekeepingServPoolPo.getServId());
        storeOrderCartPo.setValueId(housekeepingServPoolPo.getServId());
        storeOrderCartPo.setPrice("0");
        storeOrderCartPo.setCartType(StoreOrderCartDto.CART_TYPE_SERV);
        storeOrderCartPo.setRemark(housekeepingServPoolPo.getContext());
        storeOrderCartPo.setProdName(housekeepingServPoolPo.getServName());
        storeOrderCartPo.setAreaCode(housekeepingServPoolPo.getAreaCode());
        storeOrderCartPo.setDeduction("0");
        flag = storeOrderCartInnerServiceSMO.saveStoreOrderCart(storeOrderCartPo);
        if (flag > 0) {
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存电话家政服务工单池失败-4");
    }
}
